

let activity_tag = 'ysf_vip'
let request_log = ""
let request_key = activity_tag+'_award'
let skill_count_key = activity_tag+'_award_count'
let request_log_key = activity_tag+'_award_log'
let skill_name = 'skill_name'
let skill_result = false
let default_count = 60
function rewrite_body(body) {
    console.log(body);
    body =  JSON.parse(body);
    if (body.respMsg){
        body.respMsg = "抓到你啦";
    }
    body =  JSON.stringify(body);
    return body
}  

function resp_case(request_value,count,res) {
    console.log('请求结果：');
    console.log(res.headers.date);
    console.log(res.data);
    if(res.data.respCd=="00"){
        skill_result = true
       
    }else{
        setTimeout(axios_skill,1000,request_value,count);
        // axios_skill(request_value,count)
    }
    // if(res.data.respCd=="01"){
    //     skill_result = true
       
    // }
    // else if(res.data.subCodeMsg.includes("已抢完")){
    //     $feed.push(skill_name+"秒杀结果：", res.data.subCodeMsg)
    // }
  
    if(!request_log.includes(res.data.respMsg)){
        request_log= request_log + res.headers.date+":"+res.data.respMsg +"\n"
        $store.set(request_log_key, request_log) 
    }
  
}
function error_case(request_value,count,e) {
    console.log(e.message)
    setTimeout(axios_skill,1000,request_value,count);
}

//上面是暂时需要适配的部分
!(async () => {
    if (typeof $request !== "undefined") {
  console.log(`开始获取请求参数`);
   get_request_key();
    body =  JSON.parse(body);
    let body = rewrite_body($response.body);
    body =  JSON.stringify(body);
    $done({ body })
}else {
    console.log(`开始执行请求`);
    request_log  = $store.get(skill_name)+'\n';
    $store.set(request_log_key, request_log) 
    let request_value = $store.get(request_key);
    if(request_value&&request_value!=""){
        //  $store.delete没有暴露出来，无法使用，兼容下
        // $store.set(request_key, "")  
        let count = $store.get(skill_count_key);
        if (!count){
            $store.set(skill_count_key, default_count)  
            count = default_count
        }
        // setTimeout(axios_skill,900,request_value,count);
        axios_skill(request_value,count);
        
    }else{
        console.log(request_key+`不存在`);
    }
    }
    })()
   .catch((e) =>console.log(e))
   .finally(() =>$done())

function axios_skill(request_value,count){
    if(!skill_result&&count>0){
        console.log(`执行请求：`);
        count--;
        $axios(request_value)
        .then(res=>resp_case(request_value,count,res))
        .catch(e=>error_case(request_value,count,e));  
    }
  
}


function get_request_key() {
    $store.set(request_key, $request) 
}
